package com.mingqi.enetconnected.view.Dialog.tree.indicator;

import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.view.Dialog.tree.IDynamicTreeData;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yaxi
 * @date 2018/9/18
 */
public class IndicatorAdapter extends AbstractAdapter<IDynamicTreeData> {

    private List<IDynamicTreeData> mDatas;
    private List<View> mViews;

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public View getView(int position, ViewGroup parent) {
        if (position < mViews.size() && mViews.get(position) != null){
            TextView view = (TextView) mViews.get(position);
            view.setText(mDatas.get(position).getShowLeft());
            return view;
        }
        return getTextView(position, parent);
    }

    @NonNull
    private TextView getTextView(int position, ViewGroup parent) {
        IDynamicTreeData data = mDatas.get(position);
        TextView view = new TextView(parent.getContext());
        view.setText(data.getShowLeft());
        view.setSingleLine(true);
        view.setGravity(Gravity.CENTER);
        view.setTextColor(parent.getContext().getResources().getColor(R.color.color_black_ff333333));
        int left = DeviceUtils.dp2px(parent.getContext(),5);
        int right = DeviceUtils.dp2px(parent.getContext(),5);
        view.setPadding(left,10,right,10);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        mViews.add(position,view);
        return view;
    }


    public int getSize(){
        return mViews.size();
    }

    @Override
    public View addView(int position, ViewGroup parent){
        return getTextView(position, parent);
    }

    @Override
    public void removeView(int position){
        mViews.remove(position);
    }


    public IndicatorAdapter(List<IDynamicTreeData> datas) {
        this.mDatas = datas;
        mViews = new ArrayList<>();
    }

    @Override
    public IDynamicTreeData getData(int position) {
        return mDatas.get(position);
    }

    @Override
    public void addData(IDynamicTreeData iDynamicTreeData) {
        if (iDynamicTreeData.isLastLevel()){
            // TODO: 2018/9/18 remove "please select"
        }
    }

    @Override
    public void setData(int position, IDynamicTreeData iDynamicTreeData) {
        mDatas.set(position,iDynamicTreeData);
        mObservable.notifyItemChanged(position);
    }


    @Override
    public void addData(int position, IDynamicTreeData iDynamicTreeData){
        mDatas.add(position,iDynamicTreeData);
        mObservable.notifyItemAdd(position);
    }

    public void removeData(int position){
        mDatas.remove(position);
        mObservable.notifyItemRemoved(position);
    }

    public void removeRangeData(int start,int count){
        for (int i = 0; i < count; i++) {
            mDatas.remove(start);
        }
        mObservable.notifyItemRangeChanged(start, count);
    }

}
